package main

import (
	"fmt"
	"os"
	"strconv"
)

func seq(max int) <-chan int {
	ch:=make(chan int)
	go func() {
		for i:=2; i<max; i++ {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

func filter(in <-chan int,  prime int) <-chan int {
	out:=make(chan int)

	go func() {
		for v:= range in {
			if v % prime != 0 {
				out <- v
			}
		}
		close(out)
	}()

	return out
}

func sieve(max int) <-chan int {
	out:=make(chan int)

	go func() {
	ch:=seq(max)
		for {
			v,ok:= <-ch
			if !ok {
				close(out)
				break
			}
			ch=filter(ch, v)
			out <- v
		}
	}()

	return out
}

func main() {
	max:=1000
	if len(os.Args)>1 {
		var err error
		max,err=strconv.Atoi(os.Args[1])
		if err!=nil { max=1000}
	}

	for v:= range sieve(max) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
